package com.hoas.comparedb.core;

import java.util.*;

public class CompareServiceLoader {

    private static volatile CompareServiceLoader LOADER;
    private final List<DBCompareBuilder> builders;

    private CompareServiceLoader() {

        ServiceLoader<DBCompareBuilder> loader = ServiceLoader.load( DBCompareBuilder.class);
        List<DBCompareBuilder> list = new ArrayList<>();
        for (DBCompareBuilder landingOnTheMoon : loader) {
            list.add(landingOnTheMoon);
        }
        builders = list;

    }

    /**
     * LandingOnTheMoonLoader 单例加载
     * */
    public static CompareServiceLoader getLoader() {
        if (LOADER == null) {
            synchronized (CompareServiceLoader.class) {
                if (LOADER == null) {
                    LOADER = new CompareServiceLoader();
                }
            }
        }
        return LOADER;
    }

    public static DBCompareBuilder getBuilder(String type) {
        return getLoader().builders.stream().filter( r -> r.getType().equals( type )).findAny().get();
    }

}
